package com.yaowk.service.system.service.impl;

import com.yaowk.api.system.model.Role;
import com.yaowk.api.system.service.IRoleService;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.system.dao.RoleRepository;
import com.yaowk.service.system.dao.UserRepository;
import com.yaowk.service.system.entity.SysMenu;
import com.yaowk.service.system.entity.SysRole;
import com.yaowk.service.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.transaction.Transactional;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role, SysRole, Integer> implements IRoleService {

    @Autowired
    private RoleRepository dao;
    @Autowired
    private UserRepository userDao;

    @Override
    public Role save(Role model) {
        if (isNotEmpty(model.getId())) {
            SysRole sysRole = dao.findOne(model.getId());
            if (isNotEmpty(model.getName()))
                sysRole.setName(model.getName());
            if (isNotEmpty(model.getDescription()))
                sysRole.setDescription(model.getDescription());
            if (isNotEmpty(model.getStatus()))
                sysRole.setStatus(model.getStatus());

            model = entity2model(dao.save(sysRole));
        } else {
            model = entity2model(dao.save(model2Entity(model)));
        }
        return model;
    }

    @Override
    @Transactional
    public void menuEdit(Integer roleId, List<Integer> menuIds) {
        SysRole sysRole = dao.findOne(roleId);
        Set<SysMenu> menus = new HashSet<>();
        sysRole.setMenus(menus);
        if (!CollectionUtils.isEmpty(menuIds)) {
            for (Integer menuId : menuIds) {
                SysMenu sysMenu = new SysMenu();
                sysMenu.setId(menuId);
                menus.add(sysMenu);
            }
        }
        dao.save(sysRole);
    }

    @Override
    @Transactional
    public void menuAdd(Integer roleId, Integer menuId) {
        SysRole sysRole = dao.findOne(roleId);
        Set<SysMenu> menus = sysRole.getMenus();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(menuId);
        menus.add(sysMenu);
        dao.save(sysRole);
    }

    @Override
    @Transactional
    public List<Role> findRolesByUserId(Integer id) {
        SysUser sysUser = userDao.findOne(id);
        Set<SysRole> roles = sysUser.getRoles();
        return entity2Model(roles);
    }

    @Override
    protected BaseRepository<SysRole, Integer> getDao() {
        return dao;
    }

    @Override
    protected SysRole model2Entity(Role model) {
        SysRole sysRole = null;
        if (!ObjectUtils.isEmpty(model)) {
            sysRole = new SysRole();
            sysRole.setCreateTime(model.getCreateTime());
            sysRole.setCreateUserId(model.getCreateUserId());
            sysRole.setDescription(model.getDescription());
            sysRole.setId(model.getId());
            sysRole.setName(model.getName());
            sysRole.setStatus(model.getStatus());
        }
        return sysRole;
    }

    @Override
    protected Role entity2model(SysRole entity) {
        Role role = null;
        if (!ObjectUtils.isEmpty(entity)) {
            role = new Role();
            role.setCreateTime(entity.getCreateTime());
            role.setCreateUserId(entity.getCreateUserId());
            role.setDescription(entity.getDescription());
            role.setId(entity.getId());
            role.setName(entity.getName());
            role.setStatus(entity.getStatus());
        }
        return role;
    }
}
